var Element = require('./element');
var inherits = require('../utils/inherits');
var defineComponent = require('./define-component');
var ANode = require('../parser/a-node');
var parseTemplate = require('../parser/parse-template');

function Component(options) {
    this.slotChilds = [];
    this.dataChanges = [];
    this.listeners = {};
    console.log('Component',this);
    Element.call(this,options);
}
inherits(Component, Element);
Component.prototype._type = 'component';
Component.prototype.init = function (options) {
    this.filters = this.filters || this.constructor.filters;
    this.computed = this.computed || this.constructor.computed || {};
    this.messages = this.messages || this.constructor.message || {};
    this.subTag = options.subTag;

    this._compile();
     var me = this;
     var givenANode = options.aNode;
     var protoANode = this.constructor.prototype.aNode;
     if (givenANode) {
         var givenSlots = {};
         each(givenANode.childs,function(child){

         })
     }
}
Component.prototype._compile=function(){
    var ComponentClass = this.constructor;
    var proto = ComponentClass.prototype;
    if (!proto.hasOwnProperty('_cmptReady')) {
        proto.components = ComponentClass.components || proto.components || {};
        var components = proto.components;
        for (var key in components) {
            var componentClass = components[key];
            if(typeof componentClass==='object'){
                components[key] = defineComponent(componentClass);
            }else if(componentClass==='self'){
                components[key] = ComponentClass;
            }
        }
        proto._cmtReady = 1;
    }
    if(!proto.hasOwnProperty('_compiled')){
        proto.aNode=new ANode();

        var tpl = ComponentClass.template || proto.template;
        if(tpl){
            var aNode = parseTemplate(tpl);
        }
    }
}
Component.prototype._callHook =
    Component.prototype._toPhase = function (name) {
        Node.prototype._toPhase.call(this, name);
        if (typeof this[name] === 'function') {
            this[name].call(this);
        }
    }
    Component.prototype._toAttached=function(){
        this._getEl();
        Node.prototype._toAttached.call(this);
    }